(function ($) {
    /*
     * lvGrid  0.0.1 - jQuery Grid
     * Copyright (c) 2009
     * Dual licensed under the MIT and GPL licenses
     * http://www.opensource.org/licenses/mit-license.php
     * http://www.gnu.org/licenses/gpl.html
     * Date: 2009-05-11 rev 91
     */
    $.ajaxSetup({
        async: false
    });
    $.getScript('js/jquery.blockUI.js');
    $.ajaxSetup({
        async: true
    });
    $.extend($.fn, {
        ///<summary>
        /// apply a slider UI
        ///</summary>
        lvGird: function(setting) {
            var _this = this;
            var ps = $.extend({
                mode: {
                    'import':'重要'
                },
                //content holder(Object || css Selector)
                renderTo: $(document.body),
                //whether the slider can be dragged
                data_url: null,
                view_url: null,
                cols: [],
                id_field: null,
                caption: '用户列表'
            }, setting);
            var str = '<div class="bg_mode_gb"><div class="loading">数据处理中...</div><div class="bg_mode_gb_title"><h3>' +ps.caption+ '</h3></div>';
            str +=  '<div class="div-gird">';
            str += '<div class="th-header"><ul>'
            for(var j=0;j<ps.cols.length;j++){
                str +='<li style="'+(ps.cols[j].style?ps.cols[j].style:'')+'">'+ps.cols[j].caption+'</li>';
            }

            str += '</ul></div>';
            str += '<div class="div-gird-body">';
            str += '</div></div><div class="HackBox"></div></div>';
            this.html(str);
            function replaceIdstring(value, to) {
                //                alert(to['art_id']);
                var myReg = /-(\w.+)-/ig;
                var m = value.match(myReg);
                if(m) {
                    //                                                                    alert(value);
                    for (var i = 0; i < m.length; i++)
                    {
                        //                        alert(m[i]);
                        var t = m[i].replace(/-/ig, '');
                        //                                                alert(t);
                        if(t.indexOf('.') > -1) {
                            var s = t.split('.');
                            value = value.replace(m[i], to[s[0]][s[1]]);
                        }
                        else {
                            value = value.replace(m[i], to[t]);
                        }
                    //                        alert(m[i]);
                    }
                //                    alert(m.length);
                //                    alert(m.length + '|' + m[0]+ '|' + m[1]);
                }
                //                if(m) {
                //                    for (var i = 0; i < m.length; i++)
                //                    {
                //                        //   value = value.replace(/m[0]/ig, to[m[1]]);
                //                        alert(m[3]);
                //                    }
                //                    //                //                alert(m);
                //                    return value;
                //                //                return value.replace(/_id_/ig, to);
                //                }
                return value;
            }

            this.getData = function(options) {
                $.ajaxSetup({
                    async: false
                });
                if(!options) options = {
                    page:1
                };
                var returnJSON = null;
                //                $(_this.selector+' .loading').show();
                $(_this.selector).block({
                    message: '处理数据中...'
                });
                //                $(ps.renderTo)[0].disabled = true;
                $.post(ps.data_url, options, function(data){
                    //                alert(json);
                    var json = returnJSON = toJSON(data);
                    //                    alert(this.json);
                    var str = '';
                    for(var i=0;i<json.rows.length;i++) {
                        str += '<ul>';
                        for(var j=0;j<ps.cols.length;j++){
                            str += '<li style="'+(ps.cols[j].style?ps.cols[j].style:'')+'">';
                            if(ps.cols[j].view_url){
                                if(ps.cols[j].attrs) {
                                    var attr = '';
                                    for(var pp in ps.cols[j].attrs) {
                                        attr = pp + '="' +ps.cols[j].attrs[pp]+ '"';
                                    }
                                }
                                if(ps.cols[j].label.indexOf('.') > -1) {
                                    var k = ps.cols[j].label.split('.');
                                    var tt = '';
                                    //                                    alert(json.rows[i][k[0]].length);
                                    if(json.rows[i][k[0]] && json.rows[i][k[0]].length) {
                                        for(var g = 0;g<json.rows[i][k[0]].length;g++) {
                                            tt += json.rows[i][k[0]][g][k[1]]+'&nbsp;';
                                        }
                                    }
                                    else {
                                        //                                        alert(1);
                                        tt = json.rows[i][k[0]][k[1]]?json.rows[i][k[0]][k[1]]:'&nbsp;';
                                    }

                                    str +='<a '+attr+' target="'+ps.cols[j].target+'" class="'+ps.cols[j].cls+'" title="'+(ps.cols[j].title?ps.cols[j].title:tt)+'" href="'+replaceIdstring(ps.cols[j].view_url, json.rows[i])+'">';
                                    str += '<span title="'+tt+'">'+tt+'</span>';
                                    str += '</a>';
                                }
                                else {
                                    str +='<a '+attr+' target="'+ps.cols[j].target+'" class="'+ps.cols[j].cls+'" title="'+(ps.cols[j].title?ps.cols[j].title:json.rows[i][ps.cols[j].label])+'" href="'+replaceIdstring(ps.cols[j].view_url, json.rows[i])+'">' +json.rows[i][ps.cols[j].label]+ '</a>';
                                }

                                if(json.rows[i].extend_fields) {
                                    for(var ex_id = 0; ex_id < json.rows[i].extend_fields.length;ex_id++) {
                                        if(json.rows[i].extend_fields[ex_id].fieldvalue == 'y') {
                                            str +="[" +ps.mode[json.rows[i].extend_fields[ex_id].fieldname]+ "]";
                                        }
                                    }
                                }
                            }
                            else {
                                if(ps.cols[j].label.indexOf('.') > -1) {
                                    var k = ps.cols[j].label.split('.');
                                    if(json.rows[i][k[0]] && json.rows[i][k[0]].length) {
                                        for(var g = 0;g<json.rows[i][k[0]].length;g++) {
                                            str += json.rows[i][k[0]][g][k[1]]+'&nbsp;';
                                        }
                                    }
                                    else {
                                        str += json.rows[i][k[0]][k[1]]?json.rows[i][k[0]][k[1]]:'&nbsp;';
                                    }

                                }
                                else {
                                    str += json.rows[i][ps.cols[j].label];
                                }

                            }
                            str +='</li>';
                        }
                        if(ps.edit_button) {
                            str += '<li style="' +ps.edit_button.style+ '"><a target="' +ps.edit_button.target+ '" title="' +replaceIdstring(ps.edit_button.title, json.rows[i])+ '" class="' +ps.edit_button.cls +'" href="'+replaceIdstring(ps.edit_url, json.rows[i])+'">编辑</a></li>';
                            if(ps.skipedit && ps.skipedit == json.rows[i][ps.skipedit_field]) {
                            //
                            }else {
                                str += '<li><input type="checkbox" name="idset[]" class="idset" value="' +json.rows[i][ps.id_field]+ '"></li>';
                            }
                        }
                        str += '</ul>';
                    }
                    //                    alert(_this.selector);
                    $(_this.selector+ " .div-gird-body").html(str);

                    $('.div-gird-body ul').zebra('#F7F7F7', '#FFF');
                    $('.div-gird-body ul').lighten('#FFFDEE', '#DFE8F6');

                    if(ps.multiselect){
                        $('.div-gird-body ul').click(function() {
                            $(this).find('.idset').each(function(){
                                if(this.checked) {
                                    this.checked = false;
                                }
                                else {
                                    this.checked = true;
                                }
                            });
                        });
                    }
                //                    $(_this.selector+' .loading').fadeOut(500);

                //                    $.fn.pager();
                });
                $.ajaxSetup({
                    async: true
                });
                return returnJSON;
            }

            //            this.after(ps.pager());
            //            alert(ps.cols.length);
            ps.renderTo = (typeof ps.renderTo == 'string' ? $(ps.renderTo) : ps.renderTo);
            //            this.pager({page:1});
            return this;
        //            this.modal();
        },
        tb_show: function(obj) {
            $('.modal').click(function() {
                //                alert(1)
                tb_show($(this).attr('title'), $(this).attr('href'));
                return false;
            })
        },
        pager:function(options) {
            //            alert(this.ps);
            var op = {};
            for(var i in options) {
                if(i == 'page') {
                    op[i] = '[p]';
                }
                else {
                    op[i] = options[i];
                }
            }
            var opstr = new Array();
            var j = 0;
            for(var i in op) {
                //                alert(op[i]);
                opstr[j] = '\''+i+'\''+':'+'\''+op[i]+'\'';
                j++;
            }

            //            alert(opstr.join(',').toString());

            $('.pager').remove();
            var str = '<div class="pager">';
            str += '</div>';
            this.after(str);
            var json = this.getData(options);
            pager(json.records, json.size, options.page, 'pager', options.gird+".pager(\{"+ opstr.join(',').toString() +"\});", true);
            $(this.selector).unblock();
            this.tb_show();
            return this;
        }
    });
})(jQuery);
